/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using EvaluationException = System.Qizx.Api.EvaluationException;
using Comparison = System.Qizx.Util.Basic.Comparison;
using HTable = System.Qizx.Util.Basic.HTable;
using Key = System.Qizx.Util.Basic.HTable.Key;
using XQItem = System.Qizx.XQuery.XQItem;
using XQValue = System.Qizx.XQuery.XQValue;
namespace System.Qizx.XQuery.Dt
{
	
	/// <summary> Removes duplicate items from a source value.</summary>
	public class DistinctValueSequence:SingleSourceSequence
	{
		private System.Globalization.CompareInfo collator;
		private HTable seenSet = new HTable();
		private HashedItem probe;
		private Atomizer atoms;
		
		public DistinctValueSequence(XQValue source, System.Globalization.CompareInfo collator):base(source)
		{
            probe = new HashedItem(null);
			atoms = new Atomizer(source);
			this.collator = collator;
		}
		
		public override bool Next()
		{
			for (; atoms.Next(); )
			{
				item = atoms.Item;
				probe.item = item;
				if (seenSet.hasPut(probe))
				{
					return true;
				}
			}
			return false;
		}
		
		public override XQValue BornAgain()
		{
			return new DistinctValueSequence(source.BornAgain(), collator);
		}
		
		internal class HashedItem:HTable.Key
		{
			internal XQItem item;
			
			public HashedItem(XQItem item)
			{
				this.item = item;
			}

            public override int GetHashCode()
			{
				// TODO gross problem with collators: need special hash function
				// for strings
				int h = item.GetHashCode();
				return h;
			}
			
			public override bool Equals(System.Object obj)
			{
				if ((!(obj is HashedItem)))
				{
					throw new System.SystemException("oops! should be an hitem: " + obj);
				}
				XQItem other = ((HashedItem) obj).item;
				int cmp;
				try
				{
					cmp = item.CompareTo(other, null, 0);
				}
				catch (EvaluationException e)
				{
					cmp = Comparison.ERROR;
				}
				if (cmp == Comparison.EQ)
					return true;
				try
				{
					if (cmp == Comparison.FAIL)
					{
						double d1 = item.ValueAsDouble, d2 = other.ValueAsDouble;
						return d1 != d1 && d2 != d2;
					}
				}
				catch (EvaluationException ignore)
				{
					;
				}
				return false;
			}
			
			public override Key duplicate()
			{
				return new HashedItem(item);
			}
		}
	}
}